<?php namespace Admin\Controller; 

use Admin\Model\Store;

class StoreController extends AuthController
{
	protected $db;

	//构造函数
	public function __init()
	{
		$this->db =  new Store;
	}
	
    //首页显示的页面
    public function index(){
        //获取商品id
        $goods_id = Q('get.goods_id');
        //表头需要用规格的名字,合并表，取得规格名字
        $attrTitle = Db::table('shop_goods_attr g')->join('shop_attr a','g.hd_shop_attr_attr_id','=','a.attr_id')->where('hd_shop_goods_goods_id',$goods_id)->where('a.attr_type',2)->groupBy('a.attr_id')->get();
        //需要跟货品表进行数据对比，进行合并数据
        $goodsAttr = Db::table('shop_goods_attr')
            ->where('hd_shop_goods_goods_id',$goods_id)
            ->get();
        //获取货品表数据
        $arr = $this->db->getAll();
        //排序一下数组
        sort($arr);
        //拆分组合属性ID、然后组合数据
        $t = array();
        foreach ($arr as $value) {
            $tmp[] = explode('-', $value['store_attr_id']);
            //遍历后拆分的数组，压入value键
            foreach ($tmp as $k => $v) 
            {
                $value['color'] = $v[0];
                $value['size'] = $v[1]; 
            }
            $t[] = $value;
        }
        //对比，获取到规格值
        $data = array();
        foreach ($t as $value)
        {
           foreach ($goodsAttr as $v)
           {
                if($v['goods_attr_id']==$value['color'])
                {
                    $value['color_value'] = $v['attr_value'];
                }
                if($v['goods_attr_id']==$value['size'])
                {
                    $value['size_value'] = $v['attr_value'];
                }
            }
            $data[] = $value;
        }
        // p($data);
        //赋值输出
        View::with('data',$data)->with('good',$goods_id)->with('attrTitle',$attrTitle);
        View::make();
    }

    //添加数据的页面
    public function add()
    {
        if(IS_POST)
        {
            //遍历关键的组合属性ID
            $arr = array();
            foreach ($_POST['store_attr_id'] as $key => $value)
            {
                foreach ($value as $k => $v) 
                {
                    $arr[$k][] = $v;
                }
            }
            //开始组合数据   
            $data = array();
            foreach ($arr as $key => $value) 
            {
                $tmp['store_attr_id'] = implode('-',$value);
                $tmp['hd_shop_goods_goods_id'] = $_POST['hd_shop_goods_goods_id'];
                $tmp['goods_sn'] = $_POST['goods_sn'][$key];
                $tmp['store_num'] = $_POST['store_num'][$key];
                $data[] = $tmp;

            }
            //传入数据到模型方法处理
            if($this->db->store($data))
            {
                View::success('添加库存成功');
            }else{
                View::error($this->db->getError());
            }
        }
        else{
            //获取商品ID
            $goods_id = Q('get.goods_id');
            // 获取表头的标题
            $attrTitle = Db::table('shop_goods_attr g')->join('shop_attr a','g.hd_shop_attr_attr_id','=','a.attr_id')->where('hd_shop_goods_goods_id',$goods_id)->where('a.attr_type',2)->groupBy('a.attr_id')->get();
            // 获取排列的内容
            $goodsAttr = Db::table('shop_goods_attr')
            ->where('hd_shop_goods_goods_id',$goods_id)
            ->get();
            // 排列数组
            sort($goodsAttr);
            // 传值
            View::with('attrTitle',$attrTitle)->with('good',$goods_id);
            View::with('goodsAttr',$goodsAttr);
            View::make();
        }
        
    }
    public function edit()
    {
        if(IS_POST)
        {
            if($this->db->edit())
            {
                View::ajax(array('code'=>0,'message'=>'更新库存成功'));
            }else{
                View::ajax(array('code'=>1,'message'=>$this->db->getError()));
            }
        }
    }
}
